package com.example.demo.day.easyExcel.primary.day1.template.demo.write;

import com.alibaba.excel.EasyExcel;
import com.example.demo.day.easyExcel.primary.day1.part1.DemoData;
import com.example.demo.day.easyExcel.primary.day1.part1.ExcelListener;
import com.example.demo.day.easyExcel.primary.day1.part1.TestEasyExcelWrite;
//import com.sun.deploy.net.HttpResponse;
//import com.sun.deploy.net.URLEncoder;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

@Controller
public class WebTest {


    @GetMapping("/download")
    public void download(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disponsition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream        (), DemoData.class).sheet().doWrite(TestEasyExcelWrite.getData());
    }

    @PostMapping("/upload")
    @ResponseBody
    public String upload(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), DemoData.class, new ExcelListener()).sheet().doRead();
        return "success";
    }
}
